﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CustomsDocumentOA.Project.CusDocument.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CustomsDocumentOA.Project.CusDocument
{
    public class CDOA_VisitRecordAppService : AsyncCrudAppService<CDOA_Visit_Record, CDOA_VisitRecordDto, Guid, PagedResultRequestDto, UpdateCDOA_VisitRecordDto, UpdateCDOA_VisitRecordDto>
    {
        private readonly IObjectMapper _objectMapper;
        private readonly ICDOA_WorkOrderAppservice _WorkOrderAppservice;
        public CDOA_VisitRecordAppService(IRepository<CDOA_Visit_Record, Guid> repository,
            IObjectMapper objectMapper, ICDOA_WorkOrderAppservice workOrderAppservice) : base(repository)
        {
            _objectMapper = objectMapper;
            _WorkOrderAppservice = workOrderAppservice;
        }
        #region 查询拜访记录
        /// <summary>
        /// 查询拜访记录
        /// </summary>
        /// <param name="Currentpage"></param>
        /// <param name="PageSizes"></param>
        /// <param name="id">工单Id</param>
        /// <param name="userName">用户</param>
        /// <param name="customerName">客户</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CDOA_VisitRecordDto>> GetList(int Currentpage, int PageSizes, string userName = "", string customerName = "", int userid = 0)
        {
            PagedResultDto<CDOA_VisitRecordDto> resultDto = new PagedResultDto<CDOA_VisitRecordDto>();
            List<CDOA_Visit_Record> lists = new List<CDOA_Visit_Record>();
            Expression<Func<CDOA_Visit_Record, bool>> expr = o => o.Id != null;
            if (userid > 0)
            {
                expr = expr.ExpressionAnd(o => o.UserId == userid);
            }
            if (!string.IsNullOrEmpty(userName))//按用户查
            {
                expr = expr.ExpressionAnd(o => o.UserName == userName);
            }
            if (!string.IsNullOrEmpty(customerName))//按照客户查
            {
                expr = expr.ExpressionAnd(o => o.CustomerName == customerName);
            }
            if (expr != null)//按工单ID查
            {
                lists = await Repository.GetAll().Where(expr).ToListAsync();

            }
            lists = lists.OrderByDescending(o => o.CreationTime).ToList();
            var dtos = lists.Skip((Currentpage - 1) * PageSizes).Take(PageSizes).ToList();
            resultDto.Items = _objectMapper.Map<List<CDOA_VisitRecordDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 折叠代码  添加客户追踪记录
        /// <summary>
        /// 添加客户追踪记录
        /// </summary>
        /// <param name="insertRecord"></param>
        /// <returns></returns>
        //public async Task<CDOA_VisitRecordDto> InsertVisitRecor(InsertRecordDto insertRecord)
        //{
        //    UpdateCDOA_VisitRecordDto dto = new UpdateCDOA_VisitRecordDto();
        //    dto.CustomerName = insertRecord.CustomerName;//客户名
        //    dto.DealTime = insertRecord.DealTime;//预计成交时间
        //    dto.UserName = insertRecord.UserName;//用户名
        //    dto.WorkOrderName = insertRecord.WorkOrderName;//工单名字
        //    dto.ImageUrl = insertRecord.ImageUrl;//图片
        //    dto.RecordInfo = insertRecord.RecordInfo;//内容
        //    var insert = await base.CreateAsync(dto);
        //    return insert;
        //}
        #endregion

        #region 今日新增拜访
        /// <summary>
        /// 今日新增拜访
        /// </summary>
        /// <param name="CurrentPage"></param>                          
        /// <param name="PageSizes"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<CDOA_VisitRecordDto>> GetVisitRecordCount(long id, int CurrentPage, int PageSizes)
        {
            PagedResultDto<CDOA_VisitRecordDto> resultDto = new PagedResultDto<CDOA_VisitRecordDto>();
            List<CDOA_Visit_Record> lists = new List<CDOA_Visit_Record>();
            lists = await Repository.GetAll().Where(it => it.CreatorUserId == id && it.CreationTime.Date == DateTime.Now.Date).ToListAsync();
            lists = lists.OrderByDescending(it => it.CreationTime).ToList();
            var dtos = lists.Skip((CurrentPage - 1) * PageSizes).Take(PageSizes).ToList();
            resultDto.Items = _objectMapper.Map<List<CDOA_VisitRecordDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        #region 批量删除
        //public async Task DeleteList(List<Guid> id)
        //{
        //    await Repository.DeleteAsync(it => id.Contains(it.Id));
        //}
        #endregion
        public async Task DeleteList(string id)
        {
            string[] obj = id.Split(",");
            for (int i = 0; i < obj.Length; i++)
            {
                await Repository.DeleteAsync(Guid.Parse(obj[i]));
            }
        }
        #endregion
    }
}
